﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Pikapika.Models;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using System.Text.RegularExpressions;

namespace Pikapika
{
    public static class ModelRouteExtensions
    {
        public static MvcHtmlString ActionLinkForModel(this HtmlHelper html, Novel novel, IDictionary<string, object> htmlAttributes = null)
        {
            return ActionLinkForModel(html, novel, novel.Name.Trim(), htmlAttributes);
        }
        public static MvcHtmlString ActionLinkForModel(this HtmlHelper html, Novel novel, string text, IDictionary<string, object> htmlAttributes = null)
        {
            return html.ActionLink(
                       text,
                       "Details",
                       "Novel",
                       CreateRouteValues(novel),
                       htmlAttributes);
        }

        public static MvcHtmlString ActionLinkForModel(this HtmlHelper html, NovelChapter chapter, Novel parentNovel = null, NovelVolume parentVolume = null)
        {
            return ActionLinkForModel(html, chapter, null, parentNovel, parentVolume);
        }
        public static MvcHtmlString ActionLinkForModel(this HtmlHelper html, NovelChapter chapter, IDictionary<string, object> htmlAttributes = null, Novel parentNovel = null, NovelVolume parentVolume = null)
        {
            return ActionLinkForModel(html, chapter, chapter.Name, htmlAttributes, parentNovel, parentVolume);
        }
        public static MvcHtmlString ActionLinkForModel(this HtmlHelper html, NovelChapter chapter, string text, IDictionary<string, object> htmlAttributes = null, Novel parentNovel = null, NovelVolume parentVolume = null)
        {
            return html.ActionLink(
                       text,
                       "Read",
                       "Novel",
                       CreateRouteValues(chapter, parentNovel, parentVolume),
                       htmlAttributes);
        }
        public static string UrlForModel(this UrlHelper url, Novel novel)
        {
            return url.Action(
                       "Details",
                       "Novel",
                       CreateRouteValues(novel));
        }

        public static string UrlForModel(this UrlHelper url, NovelChapter chapter, Novel parentNovel = null, NovelVolume parentVolume = null)
        {
            return url.Action(
                       "Read",
                       "Novel",
                       CreateRouteValues(chapter, parentNovel, parentVolume));
        }

        private static string SanitizeRouteValue(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return string.Empty;
            }
            value = value.Trim();
            return Regex.Replace(value, @"\s+", " ");
        }

        private static RouteValueDictionary CreateRouteValues(Novel novel)
        {
            return new RouteValueDictionary { { "id", novel.Id },
                       		                  { "novelName", SanitizeRouteValue(novel.Name) } };
        }
        private static RouteValueDictionary CreateRouteValues(NovelChapter chapter, Novel parentNovel, NovelVolume parentVolume)
        {
            return new RouteValueDictionary
                        { {"id", chapter.Id},
                       	    {"novelName", SanitizeRouteValue((parentNovel ?? (parentVolume ?? chapter.NovelVolume).Novel).Name)},
                       	    {"volumeName", SanitizeRouteValue((parentVolume ?? chapter.NovelVolume).Name)},
                       	    {"chapterName", SanitizeRouteValue(chapter.Name)} };
        }
    }
}